package com.feihong.txgw.tx_precenter;

import com.feihong.txgw.inter.contract.mendian.IMenDainContract;
import com.feihong.txgw.model.bean.nearby.NearbyModel;
import com.feihong.txgw.tx_model.MenDianMainModel;

import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName MenDianMainPrecenter
 * @package com.feihong.txgw.tx_precenter
 * @data 2018/5/4 19:45
 * @Describe
 * @Change
 */
public class MenDianMainPrecenter extends IMenDainContract.AbMenDianMainPrecenter {
    private int mCurrentPageNum = 1;
    private boolean isFullPage = true;
    @Override
    public void loadMenDianInfo(String url, Map<String, String> mParams) {
        mParams.put("p",String.valueOf(mCurrentPageNum));
        mIModel.getLoadMenDianInfo(url,mParams).subscribe(new Observer<NearbyModel>() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(NearbyModel o) {
                if(o.getCode() == 200){
                    List<NearbyModel.DataBean.ListBean> mStoreDatas =  o.getData().getList();
                    if(isFullPage && mCurrentPageNum <= 1){
                        isFullPage = mStoreDatas.size()  > 3;
                    }
                    mCurrentPageNum ++;
                    mIView.loadInfoResult(isFullPage,mStoreDatas);
                }else if(o.getCode() == 300){
                    mIView.loadEnd();
                }else {
                    mIView.loadOtherError(o.getMsg());
                }
            }

            @Override
            public void onError(Throwable e) {
                mIView.loadDataError();
            }

            @Override
            public void onComplete() {
                mIView.loadDataComplated();
            }
        });
    }

    @Override
    public void loadMenDianInfoFirs(String url, Map<String, String> mParams) {
        isFullPage = true;
        mCurrentPageNum = 1;
        loadMenDianInfo(url,mParams);
    }

    @Override
    public IMenDainContract.IMenDianMainModel getModel() {
        return MenDianMainModel.create();
    }

    public static MenDianMainPrecenter create(){
        return new MenDianMainPrecenter();
    }
    @Override
    public void onStart() {

    }
}
